/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.calendar.eventeditor;

import java.util.Date;
import java.util.TimeZone;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.DateField;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import mtt.calendar.eventeditor.events.ShowEventEditorView;
import mtt.calendar.weekview.events.ShowWeekViewEvent;
import mtt.rms.dataObjects.Event;
import mtt.rms.recordstores.calendar.events.AddEventToRmsEvent;
import mtt.rms.recordstores.calendar.events.DeleteEventFromRmsEvent;
import mtt.rms.recordstores.calendar.events.UpdateEventInRmsEvent;
import hmvc.j2me.controller.AController;
import hmvc.j2me.events.IEvent;
import hmvc.j2me.events.ShowDisplayAbleEvent;
import hmvc.j2me.events.TerminateEvent;

/**
 * 
 * 
 * @author jens.meiss
 */
public class EventEditorController extends AController implements
		CommandListener {

	private EventEditorModel model;

	private EventEditorView view;
	
	public EventEditorController() {
		
		setModel(new EventEditorModel());
		getModel().setEvent(new Event());
		
		setView(new EventEditorView());
		
	}
	
	public EventEditorController(Event event) {
		
		setModel(new EventEditorModel());
		getModel().setEvent(event);
		getModel().setNewEvent(false);
		
		setView(new EventEditorView());
		
	}

	public void commandAction(Command c, Displayable d) {

		if (c == getView().getCommandStore()) {

			if (getModel().isNewEvent()) {
				store();
			} else {
				update();
			}
			

		} else if (c == getView().getCommandDelete()) {

			delete();

		}

		fireEventUp(new ShowWeekViewEvent());
		fireEvent(new TerminateEvent(), false, true, false);

	}

	private void update() {
		fireEventUp(new UpdateEventInRmsEvent(getEvent()));
	}

	private void delete() {
		if (getEvent().getRecordId() > 0) {
			fireEventUp((new DeleteEventFromRmsEvent(getEvent())));
		}
	}

	private Event getEvent() {

		Event event = getModel().getEvent();

		event.setDay((byte) getView().getChoiceGroupDay().getSelectedIndex());
		
		event.setInfoText(getView().getTextFieldInfoText().getString());

		event.setIntervall(Byte.parseByte(getView().getTextFieldIntervall()
				.getString()));

		event.setName(getView().getTextFieldName().getString());

		event.setPlace(getView().getTextFieldOrt().getString());

		event.setTimeBeginOfEvent((int) getView().getDateFieldTimeOfBegin()
				.getDate().getTime());

		event.setTimeEndOfEvent((int) getView().getDateFieldTimeOfEnd()
				.getDate().getTime());

		event.setTutor(getView().getTextFieldTutor().getString());

		event.setType(getView().getChoiceGroupTyp().getSelectedIndex());

		return event;
	}

	private EventEditorModel getModel() {
		return model;
	}

	private EventEditorView getView() {
		return view;
	}

	public void init() {

		Form form = new Form("MTT - Termindetails");
		form.setCommandListener(this);
		getView().setForm(form);

		Command commandBack = new Command("zurück", Command.BACK, 0);
		getView().setCommandBack(commandBack);

		Command commandStore = new Command("Speichern", "Änderungen Speichern",
				Command.SCREEN, 1);
		getView().setCommandStore(commandStore);

		Command commandDelete = new Command("Löschen", "Termin löschen",
				Command.SCREEN, 1);
		getView().setCommandDelete(commandDelete);

		Event event = getModel().getEvent();
		
		/*
		 * Textfelder füllen
		 */
		TextField textFieldName = new TextField("Name: ", event.getName(), 25,
				TextField.ANY);
		getView().setTextFieldName(textFieldName);
		
		TextField textFieldOrt = new TextField("Ort: ", event.getPlace(), 25,
				TextField.ANY);
		getView().setTextFieldOrt(textFieldOrt);
		
		TextField textFieldTutor = new TextField("Dozent: ", event.getTutor(),
				25, TextField.ANY);
		getView().setTextFieldTutor(textFieldTutor);

		/*
		 * ChoiceGroup Typ erstellen
		 */
		ChoiceGroup choiceGroupTyp = new ChoiceGroup("Typ: ", ChoiceGroup.POPUP);
		getView().setChoiceGroupTyp(choiceGroupTyp);
		choiceGroupTyp.append("VO - Vorlesung Obligatorisch", null);
		choiceGroupTyp.append("VW - Vorlesung Wahlpflicht", null);
		choiceGroupTyp.append("PO - Praktikum Obligatorisch", null);
		choiceGroupTyp.append("PW - Praktikum - Wahlpflicht", null);
		choiceGroupTyp.append("ÜO - Übung Obligatorisch", null);
		choiceGroupTyp.append("LT - Lerngruppen-Event", null);
		choiceGroupTyp.append("Undefinierter Termin", null);
		choiceGroupTyp.setSelectedIndex(event.getType(), true);

		ChoiceGroup choiceGroupDay = new ChoiceGroup("Tag: ", ChoiceGroup.POPUP);
		getView().setChoiceGroupDay(choiceGroupDay);
		choiceGroupDay.append("Montag", null);
		choiceGroupDay.append("Dienstag", null);
		choiceGroupDay.append("Mittwoch", null);
		choiceGroupDay.append("Donnerstag", null);
		choiceGroupDay.append("Freitag", null);
		choiceGroupDay.append("Samstag", null);
		choiceGroupDay.append("Sonntag", null);
		choiceGroupDay.setSelectedIndex(event.getDay(), true);
		
		DateField dateFieldTimeOfBegin = new DateField("Uhzeit begin:",
				DateField.TIME, TimeZone.getDefault());
		dateFieldTimeOfBegin.setDate(new Date(event.getTimeBeginOfEvent()));
		getView().setDateFieldTimeOfBegin(dateFieldTimeOfBegin);
		
		DateField dateFieldTimeOfEnd = new DateField("Uhzeit ende:",
				DateField.TIME, TimeZone.getDefault());
		dateFieldTimeOfEnd.setDate(new Date(event.getTimeEndOfEvent()));
		getView().setDateFieldTimeOfEnd(dateFieldTimeOfEnd);

		/*
		 * ChoiceGroup Intervall
		 */
		TextField textFieldIntervall = new TextField("Intervall in Wochen: ",
				"0" + event.getIntervall(), 2, TextField.DECIMAL);
		getView().setTextFieldIntervall(textFieldIntervall);

		TextField textFieldInfoText = new TextField("zusatz Informationen",
				event.getInfoText(), 255, TextField.ANY);
		getView().setTextFieldInfoText(textFieldInfoText);

		form.append(textFieldName);
		form.append(textFieldOrt);
		form.append(textFieldTutor);
		
		form.append(choiceGroupTyp);
		form.append(choiceGroupDay);
		
		form.append(dateFieldTimeOfBegin);
		form.append(dateFieldTimeOfEnd);
		
		form.append(textFieldIntervall);
		form.append(textFieldInfoText);

		form.addCommand(commandBack);
		form.addCommand(commandDelete);
		form.addCommand(commandStore);

		form.addCommand(commandBack);
		form.addCommand(commandStore);
		form.addCommand(commandDelete);

		register(ShowEventEditorView.class);

	}

	public void receiveEvent(IEvent event) {

		if (event instanceof ShowEventEditorView) {
			fireEventUp(new ShowDisplayAbleEvent(getView().getDisplayable()));
		}

	}

	private void setModel(EventEditorModel model) {
		this.model = model;
	}

	private void setView(EventEditorView view) {
		this.view = view;
	}

	private void store() {
		fireEventUp(new AddEventToRmsEvent(getEvent()));
	}

	public void terminate() {
	}
}
